#ifndef bmipanel_h
#define bmipanel_h

#include "UtilityPanel.h"
#include "../TextField.h"
#include <iostream>

using namespace std;

class BMIPanel: public UtilityPanel {
private:
	SelectableUtilityButton* standard;
	SelectableUtilityButton* metric;
	UtilityButton* calcButton;
	TextField* heightField;
	TextField* weightField;

	int spacerH;
	int spacerV;

	enum {
		STANDARD, METRIC, CALCULATE
	};

public:
	BMIPanel() :
		UtilityPanel(T("BMI Calculator")), spacerH(0) {
		standard = new SelectableUtilityButton(STANDARD, this, T("Standard"));
		metric = new SelectableUtilityButton(METRIC, this, T("Metric"));
		metric->setSelected(true);

		heightField = new TextField(T("Height: "));
		weightField = new TextField(T("Weight: "));

		calcButton = new UtilityButton(CALCULATE, this, T("Calculate"));

		addAndMakeVisible(standard);
		addAndMakeVisible(metric);
		addAndMakeVisible(heightField);
		addAndMakeVisible(weightField);
		addAndMakeVisible(calcButton);

	}

	~BMIPanel() {
		delete standard;
		delete metric;
		delete heightField;
		delete weightField;
		delete calcButton;
	}

	void paintPanel(Graphics& g) {
		g.setColour(Colours::lightgrey);
		g.drawHorizontalLine((int)(1.5f * spacerV), spacerH, getWidth() - spacerH);
		g.drawHorizontalLine((int)(3.5f * spacerV), spacerH, getWidth() - spacerH);
		g.drawHorizontalLine((int)(6.5f * spacerV), spacerH, getWidth() - spacerH);
		g.setColour(Colours::black);
		//		g.drawSingleLineText(text, tenthSpacer, 380);
		String heightUnit(metric->isSelected() ? T("m") : T("in"));
		String weightUnit(metric->isSelected() ? T("kg") : T("lb"));

		String text = calculate();
		g.drawFittedText(text, 0, 8 * spacerV, getWidth(), getHeight() - 8 * spacerV,
				Justification::centred, 1);
		g.setFont(Font(Font::getDefaultSansSerifFontName(), 16, Font::plain));
		g.drawSingleLineText(heightUnit, heightField->getRight() + 3, heightField->getY()
				+ heightField->getHeight() / 2 + 4);
		g.drawSingleLineText(weightUnit, weightField->getRight() + 3, weightField->getY()
				+ weightField->getHeight() / 2 + 4);
	}

	void resized() {
		spacerH = int(getWidth() * 0.1);
		spacerV = int(getHeight() * 0.1);
		int buttonWidth = (getWidth() - 3 * spacerH) / 2;

		metric->setBounds(spacerH, 2 * spacerV, buttonWidth, spacerV);
		standard->setBounds(getWidth() - spacerH - buttonWidth, 2 * spacerV, buttonWidth, spacerV);

		heightField->setBounds(spacerH, 4 * spacerV, getWidth() - 4 * spacerH, spacerV);
		weightField->setBounds(spacerH, 5 * spacerV, getWidth() - 4 * spacerH, spacerV);
		calcButton->setBounds(spacerH, 7 * spacerV, getWidth() - 2 * spacerH, spacerV);
	}

	void buttonClicked(int buttonID) {
		if (buttonID == STANDARD) {
			standard->setSelected(true);
			metric->setSelected(false);
		} else if (buttonID == METRIC) {
			metric->setSelected(true);
			standard->setSelected(false);
		} else if (buttonID == CALCULATE) {
		}
		repaint();
	}

	String calculate() {
		float value = weightField->getValue() / (heightField->getValue() * heightField->getValue());
		if(standard->isSelected())
		{
			/* Account for height being in inches */
			value *= 703;
		}
		if (value > 0 && value < 1000)
			return String(value);
		else
			return String(T(" -- "));
	}
};
#endif
